home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / dbwrendr.zip / SOURCE / RAY.C < prev    next >
Text File  |  1989-05-14  |  21KB  |  594 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  *                                                                      *
  24.  * Authors:                                                             *
  25.  *      DBW - David B. Wecker                                           *
  26.  *      JHL - John H. Lowery, IBM-PC/Microsoft C port                   *
  27.  *                                                                      *
  28.  * Versions:                                                            *
  29.  *      V1.0 870125 DBW - First released version                        *
  30.  *      V1.1 (IBM) 890101 JHL - version 1.0 for the IBM-PC/VGA/MCGA     *
  31.  *                                                                      *
  32.  ************************************************************************/
  33.  
  34. #define MODULE_RAY
  35. #include "ray.h"
  36.  
  37. vector    base,
  38.           vx,
  39.           vy,
  40.           d,
  41.           p,
  42.           sumval,
  43.           sampleval,
  44.           varsum,
  45.           samplep,
  46.           rnd_vr,
  47.           rnd_vu,
  48.           subvr,
  49.           subvu,
  50.           old_ave,
  51.           ave_val,
  52.           eye2,
  53.           focalpoint,
  54.           lens1,
  55.           lens2,
  56.           temp1,
  57.           temp2;
  58.  
  59. float     rnd_wid,
  60.           rnd_hi,
  61.           lensx,
  62.           lensy,
  63.           focallength,
  64.           totalcount,
  65.           count,
  66.           maxcount,
  67.           d_alias,
  68.           d_bestf1,
  69.           d_bestf2;
  70.  
  71. unsigned  modulo;
  72. int       row,
  73.           col,
  74.           i,j,k,
  75.           compthresh,
  76.           compmin,
  77.           bunch,
  78.           fac1,
  79.           fac2,
  80.           bestf1,
  81.           bestf2,
  82.           spatialx,
  83.           spatialy,
  84.           pixelcompute,
  85.           pixelguess,
  86.           didguess;
  87. long      curtime,
  88.           prvtime,
  89.           nxttime,
  90.           xtrtime,
  91.           buftime,
  92.           totaltime,
  93.           totalguess,
  94.           totalcompute;
  95.  
  96. unsigned char cache[9][MAXX][3];
  97. int           computes[MAXX];
  98.  
  99.  
  100. guess2(col,sum,val,dis,xdir,ydir)
  101. int     col,*sum,dis,xdir,ydir;
  102. int     val[3];
  103. {
  104.      int i,x1,y1,x2,y2,retval,shft,isum;
  105.  
  106.      shft    = 6 - dis;
  107.      isum    = 1 << shft;
  108.      y1      = 4 - (dis * ydir);
  109.      x1      = col - (dis * xdir);
  110.      if (y1 < 0)
  111.           y1 = 0;
  112.      if (y1 > 8)
  113.           y1 = 8;
  114.      if (x1 < 0)
  115.           x1 = 0;
  116.      if (x1 >= MAXCOL)
  117.           x1 = MAXCOL-1;
  118.  
  119.      retval = 0;
  120.      y2 = y1;
  121.      while (1) 
  122.      {
  123.           if (cache[y2][x1][0] != 0xFF) 
  124.           {
  125.                for (i = 0; i < 3; i++)
  126.                     val[i] += ((int)cache[y2][x1][i]) << shft;
  127.                *sum    += isum;
  128.                retval   = 1;
  129.           }
  130.           if (y2 == 4)
  131.                break;
  132.           y2 += ydir;
  133.      }
  134.      x2 = x1;
  135.      while (1) 
  136.      {
  137.           if (cache[y1][x2][0] != 0xFF) 
  138.           {
  139.                for (i = 0; i < 3; i++)
  140.                     val[i] += ((int)cache[y1][x2][i]) << shft;
  141.                *sum    += isum;
  142.                retval   = 1;
  143.           }
  144.           if (x2 == col)
  145.                break;
  146.           x2 += xdir;
  147.      }
  148.      return(retval);
  149. }
  150.  
  151. void guess(col)
  152. int col;
  153. {
  154.      int     i,sum,dir,dis;
  155.      int     val[3];
  156.  
  157.      sum = 0;
  158.      dir = 0;
  159.      for (i = 0; i < 3; i++)
  160.           val[i] = 0;
  161.      for (dis = 1; dir != 15 && dis < 7; dis++) 
  162.      {
  163.           if ((dir & 1) != 1)
  164.                if (guess2(col,&sum,val,dis,-1,1))
  165.                     dir |= 1;
  166.           if ((dir & 2) != 2)
  167.                if (guess2(col,&sum,val,dis,1,-1))
  168.                     dir |= 2;
  169.           if ((dir & 4) != 4)
  170.                if (guess2(col,&sum,val,dis,1,1))
  171.                     dir |= 4;
  172.           if ((dir & 8) != 8)
  173.                if (guess2(col,&sum,val,dis,-1,-1))
  174.                     dir |= 8;
  175.           if (dis > 2 && ((dir & 3) == 3 || (dir & 12) == 12))
  176.                break;
  177.      }
  178.      if (sum == 0)
  179.           sum = 1;
  180.      for (i = 0; i < 3; i++) 
  181.      {
  182.           cache[4][col][i] = (unsigned char)(val[i] / sum);
  183.      }
  184. }
  185.  
  186. void do_raytrace()
  187. {
  188.  
  189.      /* set up pixel coordinate */
  190.      vecscale((float)col,vr,vx);
  191.      vecsub(base,vy,p);
  192.      vecsum(p,vx,p);
  193.  
  194.      /* Compute this next pixel.  Distribute 'n' rays to do this */
  195.      veczero(sumval);  /* start with a black pixel */
  196.      veczero(varsum);
  197.      veccopy(backgroundval,ave_val);  /* have to start someplace */
  198.      count = 0.0;
  199.      bunch = antialias;
  200.      spatialx = 0;
  201.      spatialy = 0;
  202.      curr_runs = 0;
  203.  
  204.      while (bunch > 0) 
  205.      {
  206.  
  207.           /* Cast another sample ray for this pixel into the next subregion */
  208.           /* Pick a new random perturbing vector for this sample for spatial
  209.           antialiasing */
  210.           if (count < d_alias) 
  211.           {
  212.                rnd_wid = 0.5;  /* force first bunch through region centers */
  213.                rnd_hi  = 0.5;
  214.           }
  215.           else
  216.           {
  217.                rnd_wid = rnd();
  218.                rnd_hi  = rnd();
  219.           }
  220.  
  221.           vecscale(rnd_wid / d_bestf2,vr,rnd_vr);
  222.           vecscale(rnd_hi  / d_bestf1,vu,rnd_vu);
  223.           vecscale((float) spatialx / d_bestf2,vr,subvr);
  224.           vecscale((float) spatialy / d_bestf1,vu,subvu);
  225.           vecsum(rnd_vr,subvr,samplep);
  226.           vecsum(samplep,p,samplep);
  227.           vecsub(samplep,rnd_vu,samplep);
  228.           vecsub(samplep,subvu,samplep);
  229.           veccopy(eye,eye2);
  230.           direction(eye2,samplep,d);  /* direction from lens center to pixel */
  231.  
  232.           if (aperture > 0.0) 
  233.           {
  234.                vecscale(focus,d,focalpoint);  /* relative point on focus plane */
  235.                lensx = rnd();  /* pick random point on lens */
  236.                lensy = rnd();
  237.                if (rnd() < 0.5)
  238.                     lensx = -lensx;
  239.                if (rnd() < 0.5)
  240.                     lensy = -lensy;
  241.                vecscale(lensx,lens1,temp1);  /* make into vectors in lens plane */
  242.                vecscale(lensy,lens2,temp2);
  243.                vecsum(temp1,temp2,eye2);  /* point in the lens */
  244.                direction(eye2,focalpoint,d);  /* from spot on lens to focal point */
  245.                vecsum(eye,eye2,eye2);  /* calc new 3D eye point in lens */
  246.           }
  247.  
  248.           /* allow aborting if curr_runs > max_runs */
  249.           if (setjmp(env)) 
  250.           {
  251.                computes[col] = max_runs;    /* too many computes */
  252.                cache[8][col][1] = 0;   /* make sure we don't try again */
  253.                return;
  254.           }
  255.  
  256.           /*